import type { VxeTableGridOptions } from '@vben/plugins/vxe-table';

import type { OnActionClickFn } from '#/adapter/vxe-table';
import type { WorkOrderModel } from '#/api/production/models';

import {
  useWorkOrderStatusOptions,
  useWorkOrderTypeOptions,
} from '#/api/production/models';

/**
 * 获取表格列配置。
 * @param onActionClick 表格操作按钮点击事件
 */
export function useColumns(
  onActionClick?: OnActionClickFn<WorkOrderModel>,
): VxeTableGridOptions<WorkOrderModel>['columns'] {
  return [
    { type: 'seq', width: 60 },
    { field: 'wo', title: '工单编号', width: 160 },
    { field: 'priority', title: '优先级', width: 120, dragSort: true },
    { field: 'product.name', title: '产品', minWidth: 140 },
    {
      field: 'woType',
      title: '工单类型',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useWorkOrderTypeOptions(),
      },
    },
    {
      field: 'status',
      title: '状态',
      width: 100,
      cellRender: {
        name: 'CellTag',
        options: useWorkOrderStatusOptions(),
      },
    },
    {
      field: 'planStartDate',
      title: '计划开始日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      field: 'planEndDate',
      title: '计划完成日期',
      width: 120,
      formatter: 'formatDate',
    },
    {
      cellRender: {
        attrs: {
          nameField: '',
          nameTitle: '',
          onClick: onActionClick,
        },
        props: {
          disabled: (params: any) => params.status !== 'Issued',
        },
        name: 'CellOperation',
        options: [{ code: 'schedule', text: '排产' }],
      },
      field: 'operation',
      fixed: 'right',
      title: '操作',
      width: 80,
    },
  ];
}
